public class TruncateSentence {
    public static String truncateSentence(String s, int k) {
        StringBuilder sb=new StringBuilder();
            for(int i=0;i<s.length();i++){
                if(s.charAt(i)==' '){
                    k--;
                    if(k==0) {
                        break;
                    }
                    }
                sb.append(s.charAt(i));
                }
            return sb.toString();
            }

    public static void main(String[] args) {
        System.out.println(truncateSentence("What are you doing",3));
    }
    }
